package com.school.gift.controller;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.Evaluate;
import com.school.gift.domain.Goods;
import com.school.gift.domain.Order;
import com.school.gift.model.GoodsModel;
import com.school.gift.model.OrderModel;
import com.school.gift.service.EvaluateService;
import com.school.gift.service.GoodsService;
import com.school.gift.service.OrderService;
import com.school.support.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import zhibi.common.support.annocation.request.LoginInfo;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.util.IDUtil;
import zhibi.common.util.io.FileUtil;

import java.io.IOException;
import java.util.Date;

/**
 * Created by 执笔 on 2017/3/6.
 */
@Controller
@RequestMapping("order")
@LoginInfo(true)
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private EvaluateService evaluateService;

    @RequestMapping("myOrder")
    public String myOrder(Model model, Page page) {
        Example example = ExampleUtil.getInstance()
                .addParam("o.user_id", sessionUser().getId())
                .addOrder("o.addtime")
                .getExample();
        PageInfo<OrderModel> pageInfo = orderService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "order/list";
    }

    @RequestMapping("start")
    public String start(Integer goodsId, Integer num, Model model) {
        GoodsModel goodsModel = goodsService.selectModelById(goodsId);
        if (null == goodsModel) {
            throw new MessageException("该礼品不存在");
        }
        model.addAttribute(goodsModel);
        model.addAttribute("num", num);
        return "goods/order";
    }


/**
 * 发起订单
 * @param  icon        [description]
 * @param  order       [description]
 * @return             [description]
 * @throws IOException [description]
 */
    @RequestMapping(value = "lunch", method = RequestMethod.POST)
    public String lunch(MultipartFile icon, Order order) throws IOException {
        Goods goods = goodsService.selectByPK(order.getGoodsId());
        if (null == goods) {
            throw new MessageException("该礼品不存在");
        }
        if (null != icon && !icon.isEmpty()) {
            String fileName = FileUtil.save(filePath(), icon);
            order.setFile(fileName);
        }else{
            order.setFile(goods.getFilePath());
        }
        order.setSalary(goods.getPrice());
        order.setTotal(goods.getPrice() * order.getCount());
        order.setAddtime(new Date());
        order.setUserId(sessionUser().getId());
        order.setOrderno(IDUtil.getUUID());
        orderService.insertSelective(order);
        return redirect("myOrder");
    }

/**
 * 支付订单
 * @param  id [description]
 * @return    [description]
 */
    @RequestMapping("pay")
    public String pay(Long id) {
        Order order = orderService.selectByPK(id);
        if (null != order) {
            order.setStatus(2);
            order.setPaytime(new Date());
            orderService.updateByIdSelective(order);
        }
        return redirect("myOrder");
    }

/**
 * 收货
 * @param  id [description]
 * @return    [description]
 */
    @RequestMapping("receive")
    public String receive(Long id) {
        Order order = orderService.selectByPK(id);
        if (null != order) {
            order.setStatus(4);
            order.setReceivetime(new Date());
            orderService.updateByIdSelective(order);
        }
        return redirect("myOrder");
    }

/**
 * 订单详情
 */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        OrderModel orderModel = orderService.selectModelById(id);
        model.addAttribute(orderModel);
        Evaluate evaluate = evaluateService.selectByOrderId(id);
        model.addAttribute("evaluate",evaluate);
        return "order/detail";
    }
}
